/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Action;
import ActionForm.*;
import bean.*;
import java.util.*;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
/**
 *
 * @author cpn02
 */
public class ReduceCartAction extends Action {

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        CartForm f = (CartForm)form;
        long id = Long.parseLong(f.getIDpro());
        ArrayList<CartBean> carts = (ArrayList<CartBean>)request.getSession().getAttribute("carts");
        Iterator<CartBean> it = carts.iterator();
        ArrayList<CartBean> tem = new ArrayList<CartBean>();
        CartBean cart;
        while(it.hasNext())
        {
            cart = it.next();
            if(cart.getPro().getIDpro()==id)
            {
                cart.setAmount(cart.getAmount()-1);
                if(cart.getAmount()==0)
                    tem.add(cart);
            }
        }
        carts.removeAll(tem);
        return mapping.findForward("success");
    }

}
